package com.blockchain.console.servlets.financial.tx;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.enums.WithdrawStatus;
import com.blockchain.console.service.financial.TxglManage;
import com.blockchain.console.service.financial.entity.TxglRecord;
import com.blockchain.console.service.financial.query.TxglRecordQuery;
import com.blockchain.console.servlets.financial.AbstractFinancialServlet;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.query.Paging;
import com.blockchain.framework.service.query.PagingResult;
import com.blockchain.util.parser.IntegerParser;
import com.blockchain.util.parser.TimestampParser;

@Right(id = "C_FINANCIAL_EXPORTSHSB", name = "人民币提现-审核失败-导出")
public class ExportShsb extends AbstractFinancialServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request,
			HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		processPost(request, response, serviceSession);
	}

	@Override
	protected void processPost(final HttpServletRequest request,
			final HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		TxglManage extraction = serviceSession.getService(TxglManage.class);
		response.setHeader("Content-disposition", "attachment;filename="
				+ new Timestamp(System.currentTimeMillis()).getTime() + ".csv");
		response.setContentType("application/csv");
		PagingResult<TxglRecord> result = extraction.search(
				new TxglRecordQuery() {
					
					@Override
					public int getLsh() {
						return IntegerParser.parse(request.getParameter("lsh"));
					}
					
					@Override
					public String getYhm() {
						return request.getParameter("yhm");
					}
					
					@Override
					public String getXm() {
						return request.getParameter("xm");
					}
					
					@Override
					public String getYhkh() {
						return request.getParameter("yhkh");
					}
					
					@Override
					public WithdrawStatus getStatus() {
						return WithdrawStatus.SHSB;
					}

					@Override
					public Timestamp getStartExtractionTime() {
						return TimestampParser.parse(request
								.getParameter("startExtractonTime"));
					}

					@Override
					public Timestamp getEndExtractionTime() {
						return TimestampParser.parse(request
								.getParameter("endExtractionTime"));
					}

					@Override
					public int getBankId() {
						return IntegerParser.parse(request
								.getParameter("bankId"));
					}

				}, new Paging() {

					@Override
					public int getSize() {
						return Integer.MAX_VALUE;
					}

					@Override
					public int getCurrentPage() {
						return IntegerParser.parse(request
								.getParameter(PAGING_CURRENT));
					}
				});
		extraction.export(result.getItems(), response.getOutputStream(), "","shsb");
	}
}
